package com.ksyzt.ksite2.server.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ksyzt.gwt.server.common.Util;
import com.ksyzt.gwt.server.common.Utility;
import com.ksyzt.gwt.server.listener.KsyztServer;
import com.ksyzt.ksite2.client.column.ColumnType;
import com.ksyzt.ksite2.server.database.base.IConnectionPool;
import com.ksyzt.ksite2.server.database.base.MyPool;
import com.ksyzt.ksite2.server.database.dao.IATTACHMENTDAO;
import com.ksyzt.ksite2.server.database.dao.ICOLUMNDAO;
import com.ksyzt.ksite2.server.database.dao.INEWSDAO;
import com.ksyzt.ksite2.server.database.dao.IPUSERDAO;
import com.ksyzt.ksite2.server.database.dao.IVW_NEWSDAO;
import com.ksyzt.ksite2.server.database.dao.impl.ATTACHMENTDAOImpl;
import com.ksyzt.ksite2.server.database.dao.impl.COLUMNDAOImpl;
import com.ksyzt.ksite2.server.database.dao.impl.NEWSDAOImpl;
import com.ksyzt.ksite2.server.database.dao.impl.PUSERDAOImpl;
import com.ksyzt.ksite2.server.database.dao.impl.VW_NEWSDAOImpl;
import com.ksyzt.ksite2.server.listener.KSite2Server;
import com.ksyzt.ksite2.server.modles.OAImage;
import com.ksyzt.ksite2.shared.OrgType;
import com.ksyzt.ksite2.shared.PageData;
import com.ksyzt.ksite2.shared.PageNewsData;
import com.ksyzt.ksite2.shared.SysConst;
import com.ksyzt.ksite2.shared.modle.ATTACHMENTObj;
import com.ksyzt.ksite2.shared.modle.ATTACHMENTObjs;
import com.ksyzt.ksite2.shared.modle.COLUMNObj;
import com.ksyzt.ksite2.shared.modle.COLUMNObjs;
import com.ksyzt.ksite2.shared.modle.NEWSObj;
import com.ksyzt.ksite2.shared.modle.PUSERObj;
import com.ksyzt.ksite2.shared.modle.VW_NEWSObj;
import com.ksyzt.ksite2.shared.modle.VW_NEWSObjs;

/**
 * 用用程序Context
 * 
 * @author zhangjianshe@gmail.com
 * 
 */
public class Application {
	/**
	 * 数据库连接线程池
	 */
	private static IConnectionPool mPool;

	HttpServletRequest request;
	HttpServletResponse response;

	public Application(HttpServletRequest req, HttpServletResponse resp) {
		request = req;
		response = resp;
	}

	public HttpSession getSession() {
		return request.getSession(true);
	}

	public String getBasePath() {
		String path = request.getContextPath();
		String port = "";
		if (request.getServerPort() != 80) {
			port = ":" + request.getServerPort();
		}
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ port + path + "/";
		return basePath;
	}

	public String getDebug() {

		String debug = "";
		debug = this.parameterAsString(SysConst.KEY_DEBUG, "");
		if (!debug.equals("")) {
			debug = SysConst.KEY_DEBUG + "=" + debug;
		}
		return debug;
	}

	/**
	 * 获取数据库连接
	 * 
	 * @return
	 */
	public static IConnectionPool getConnection() {
		if (mPool == null) {
			ResourceBundle rb = ResourceBundle.getBundle("database");
			mPool = new MyPool(MyPool.DT_MYSQL, rb);
		}
		return mPool;
	}

	public boolean isAndroid() {
		String userAgent = request.getHeader("User-Agent");
		if (userAgent.contains("KSYZT-ANDROID")) {
			return true;
		} else {
			return false;
		}
	}

	public String[] userLogin(String un, String pwd, String code) {
		String r[] = new String[2];
		IPUSERDAO dao = new PUSERDAOImpl(Application.getConnection());
		PUSERObj user = null;
		try {
			user = dao.findByNAME(un);

			Logger.Loger(Logger.LEVEL_INFO, un, "尝试登录");
		} catch (SQLException e) {
			e.printStackTrace();
			r[0] = "0";
			r[1] = e.getMessage();
			return r;
		}
		pwd = Utility.encrypt(pwd);

		if (user == null) {
			r[0] = "0";
			r[1] = "账号不存在";
		} else if (!pwd.equals(user.getPWD())) {
			r[0] = "0";
			r[1] = "密码不匹配";

			Logger.Loger(Logger.LEVEL_INFO, un, "尝试登录失败");
		} else {
			user.setLASTTIME(Util.getCurrentSQLTimestamp());
			user.setLOGINCOUNT(user.getLOGINCOUNT() + 1);
			user.setSCORE(user.getSCORE() + 10);
			try {
				if (isAndroid()) {
					user.setMLOGINTIME(Util.getCurrentSQLTimestamp());
					user.setMSESSIONID(Util.randomString(16));
				}
				dao.updateObjectByPK(user);
				this.setCurrentUser(user);
				r[0] = "1";
				r[1] = user.toJSONString();

				Logger.Loger(Logger.LEVEL_INFO, un, "登录成功");
			} catch (SQLException e) {
				e.printStackTrace();
				r[0] = "0";
				r[1] = e.getMessage();

				Logger.Loger(Logger.LEVEL_INFO, un, "登录成功失败" + e.getMessage());
			}
		}
		return r;
	}

	public String parameterAsString(String key, String def) {
		String v = (String) request.getParameter(key);
		if (v == null)
			return def;
		try {
			v = new String(v.getBytes("iso-8859-1"), "gb2312");
			return URLDecoder.decode(v, "GB2312");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return def;
		}
	}

	public String parameterCSharpAsString(String key, String def) {
		String v = (String) request.getParameter(key);
		if (v == null)
			return def;
		else
			return v;
	}

	public String parameterUTFAsString(String key, String def) {
		String v = (String) request.getParameter(key);
		if (v == null)
			return def;
		try {
			v = new String(v.getBytes("iso-8859-1"), "UTF-8");
			return v;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return def;
		}
	}

	public Integer parameterAsInteger(String key, Integer def) {
		String v = (String) request.getParameter(key);
		if (v == null)
			return def;
		try {
			v = new String(v.getBytes("iso-8859-1"), "gb2312");
			String dev = URLDecoder.decode(v, "gb2312");
			if (dev.equals("")) {
				return def;
			} else {
				Integer i = Integer.valueOf(dev);
				return i;
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return def;
		}
	}

	public long parameterAsLong(String key, Long def) {
		String v = (String) request.getParameter(key);
		if (v == null)
			return def;
		try {
			v = new String(v.getBytes("iso-8859-1"), "gb2312");
			String dev = URLDecoder.decode(v, "gb2312");
			if (dev.equals("")) {
				return def;
			} else {
				Long i = Long.valueOf(dev);
				return i;
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return def;
		}
	}

	public Double parameterAsDouble(String key, Double def) {
		String v = (String) request.getParameter(key);
		if (v == null)
			return def;
		try {
			v = new String(v.getBytes("iso-8859-1"), "gb2312");
			String dev = URLDecoder.decode(v, "gb2312");
			Double i = Double.valueOf(dev);
			return i;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return def;
		}
	}

	public void setCurrentUser(PUSERObj user) {
		getSession().setAttribute(SysConst.ID_SESSION_USER, user);
	}

	public PUSERObj getCurrentUser() {
		return (PUSERObj) getSession().getAttribute(SysConst.ID_SESSION_USER);
	}

	public COLUMNObjs getChildColumns(int columnid) {
		ICOLUMNDAO dao = new COLUMNDAOImpl(getConnection());
		COLUMNObjs cs = new COLUMNObjs();
		try {
			cs = dao.selectByWhereTemplate(
					"where whoid=? and pid=? and display=1 order by rankval asc",
					1, columnid);
		} catch (SQLException e) {

		}
		return cs;
	}

	public ATTACHMENTObjs getAttatchment(String nid) {
		IATTACHMENTDAO dao = new ATTACHMENTDAOImpl(Application.getConnection());
		ATTACHMENTObjs atts = null;
		try {
			atts = dao.selectByWhereTemplate("where nid=?", nid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			atts = new ATTACHMENTObjs();
		}
		return atts;
	}

	public VW_NEWSObjs getColumnNewsByRank(int columnid, PageNaviData data,
			boolean content) {
		VW_NEWSObjs ns = new VW_NEWSObjs();
		IVW_NEWSDAO dao = new VW_NEWSDAOImpl(getConnection());
		try {
			int start = (int) ((int) data.getCurrentPage() * data.getPageSize());
			int end = (int) (start + data.getPageSize());
			ns = dao.getSpecificRecordByWhereTemplate(
					"where oid=? and display>0 order by rankval asc", start,
					end, columnid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ns;
	}

	public VW_NEWSObjs getColumnNewsByRank(int columnid, int count,
			boolean content) {
		VW_NEWSObjs ns = new VW_NEWSObjs();
		IVW_NEWSDAO dao = new VW_NEWSDAOImpl(getConnection());
		try {

			ns = dao.getSpecificRecordByWhereTemplate(
					"where oid=? and display>0 order by rankval asc", 0, count,
					columnid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ns;
	}

	public VW_NEWSObjs getColumnNews(int columnid, PageNaviData data,
			boolean content) {
		VW_NEWSObjs ns = new VW_NEWSObjs();
		IVW_NEWSDAO dao = new VW_NEWSDAOImpl(getConnection());
		try {
			int start = (int) ((int) data.getCurrentPage() * data.getPageSize());
			int end = (int) (start + data.getPageSize());

			ns = dao.getSpecificRecordByWhereTemplate(
					"where oid=? and display>0 order by uptop desc ,addtime desc",
					start, end, columnid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ns;
	}

	public VW_NEWSObjs getColumnNews(int columnid, int count, boolean content) {
		VW_NEWSObjs ns = new VW_NEWSObjs();
		IVW_NEWSDAO dao = new VW_NEWSDAOImpl(getConnection());
		try {

			ns = dao.getSpecificRecordByWhereTemplate(
					"where oid=? and display>0 order by addtime desc", 0,
					count, columnid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ns;
	}

	/**
	 * 获取服务器根目录
	 * 
	 * @param string
	 * @return
	 */
	public String getRealPath(String string) {
		return this.request.getSession().getServletContext()
				.getRealPath(string);
	}

	public PageNewsData getPageNewsData(int oid, int currentpage, int pagesize,
			boolean fetchContent) throws Exception {
		PageNewsData data = new PageNewsData();
		data.currentPage = currentpage;
		data.pagesize = pagesize;

		try {
			INEWSDAO dao = new NEWSDAOImpl(Application.getConnection());
			data.total = (int) dao.getCountByWhereTemplate("where oid=?", oid);

			IVW_NEWSDAO dao1 = new VW_NEWSDAOImpl(Application.getConnection());

			dao1.setSearchedCONTENT(fetchContent);

			data.news = dao1.getSpecificRecordByWhereTemplate(
					"where oid=? order by id desc", currentpage * pagesize,
					currentpage * pagesize + pagesize, oid);
		} catch (SQLException e) {
			throw new Exception(e.getMessage());
		}
		return data;
	}

	public VW_NEWSObj getNews(int id) {
		IVW_NEWSDAO dao = new VW_NEWSDAOImpl(getConnection());
		VW_NEWSObj n;
		try {
			n = dao.findByID(id);
		} catch (SQLException e) {
			n = null;
			e.printStackTrace();
		}
		return n;
	}

	public COLUMNObj getColumn(int cid) {
		ICOLUMNDAO dao = new COLUMNDAOImpl(getConnection());
		try {
			return dao.selectByPK(cid);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 服务器保存的验证码
	 * 
	 * @return
	 */
	public String getVeriCode() {
		String code = (String) getSession().getAttribute(SysConst.VERICODE);
		if (code == null) {
			code = "";
		}

		return code;
	}

	public String toDateString(Timestamp t) {
		if (t == null) {
			return "--/-/--";
		} else {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			return sf.format(new Date(t.getTime()));
		}
	}

	public String toDateTimeString(Timestamp t) {
		if (t == null) {
			return "--/-/--";
		} else {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return sf.format(new Date(t.getTime()));
		}
	}

	/**
	 * 用用程序缓存的页面数据
	 * 
	 * @return
	 */
	public PageData getApplicationPageData() {
		KSite2Server.PAGEDATA.siteInfo = KsyztServer.SITEINFO;
		return KSite2Server.PAGEDATA;
	}

	public static final int ORDER_BY_DATE = 0;
	public static final int ORDER_BY_RANK = 2;

	public String column2navi(COLUMNObj c) {
		StringBuilder sb = new StringBuilder();

		String path = c.getPATH();
		sb.append("<a href='.'>首页</a>");
		if (path != null && path.length() > 0) {
			String[] ps = path.split("/");
			String link = "";
			for (String p : ps) {
				String[] vs = p.split(";");
				if (vs.length > 1) {
					int id = Integer.valueOf(vs[1]);
					COLUMNObj c1 = getColumn(id);
					if (c1.getURL() != null && c1.getURL().length() > 0) {
						link = c1.getURL();
					}
					if (link.length() > 0) {
						sb.append("&nbsp;&gt;&nbsp;").append(
								"<a href='" + link + "'>" + vs[0] + "</a>");
					} else {
						sb.append("&nbsp;&gt;&nbsp;").append(vs[0]);
					}
				}
			}
		}
		String link = "";
		if (c.getURL() != null && c.getURL().length() > 0) {
			link = c.getURL();
		}
		if (link.length() > 0) {
			sb.append("&nbsp;&gt;&nbsp;").append(
					"<a href='" + link + "'>" + c.getNAME() + "</a>");
		} else {
			sb.append("&nbsp;&gt;&nbsp;").append(c.getNAME());
		}

		return sb.toString();
	}

	public VW_NEWSObj getFirstNews(int cid) {
		IVW_NEWSDAO dao = new VW_NEWSDAOImpl(getConnection());
		VW_NEWSObjs ns;
		try {
			ns = dao.getSpecificRecordByWhereTemplate(
					"where oid=? order by addtime desc", 0, 1, cid);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		if (ns.size() > 0) {
			return ns.get(0);
		}
		return null;
	}

	public String mainmenu() {
		StringBuilder sb = new StringBuilder();

		sb.append("<ul id='menu'>");
		COLUMNObjs main = getChildColumns(-1);

		for (COLUMNObj c : main) {
			String link = ".";
			if (c.getURL() != null && c.getURL().length() > 0) {
				link = c.getURL();
			}
			String baseurl = "";
			if (c.getURL() != null && c.getURL().length() > 0) {
				baseurl = c.getURL();
			}
			String a = "<a href='" + link + "'>" + c.getNAME() + "</a>";

			sb.append("<li>");
			sb.append(a);

			COLUMNObjs subs = getChildColumns(c.getID());
			if (subs.size() > 0) {
				sb.append("<ul id='menu" + c.getID() + "'>");
				sb.append("<div class='pad'></div>");
				int index = 0;
				for (COLUMNObj sub : subs) {
					if (sub.getURL() == null || sub.getURL().length() == 0) {
						if (baseurl.length() > 0) {
							if (baseurl.lastIndexOf("?") > 0) {
								sub.setURL(baseurl + "&subcid=" + sub.getID());
							} else {
								sub.setURL(baseurl + "?subcid=" + sub.getID());
							}
						} else {
							sub.setURL(".");
						}
					}
					String link1 = "<a href='" + sub.getURL() + "'>"
							+ sub.getNAME() + "</a>";
					if (index == 0) {
						sb.append("<li>");
						// sb.append("<img class='corner_inset_left' src='images/corner_inset_left.png' />");
						sb.append(link1);
						// sb.append("<img class='corner_inset_right' src='images/corner_inset_right.png' />");
						sb.append("</li>");
					} else {
						sb.append("<li>");

						sb.append(link1);
						sb.append("</li>");
					}

					index++;
				}
				sb.append("<li class='last'>");
				// sb.append("<img class='corner_left' src='images/corner_left.png' />");
				// sb.append("<img class='middle' src='images/menu_dot.png' />");
				// sb.append("<img class='corner_right' src='images/corner_right.png' />");
				sb.append("</li>");
				sb.append("</ul>");
			}

			sb.append("</li>");
		}

		sb.append("</ul>");
		return sb.toString();
	}

	public String singlearticle2html(int cid) {
		StringBuilder sb = new StringBuilder();
		COLUMNObj col = getColumn(cid);
		if (col == null) {
			return "<div style='border:solid 1px skyblue;padding:5px;'>系统不存在栏目:"
					+ cid + "</div>";
		}

		VW_NEWSObj n = getFirstNews(cid);
		if (n == null) {
			sb.append("<div>");
			sb.append("<h3>").append(col.getNAME()).append("</h3>");
			sb.append("<div>这是一个文章类型的节点，请添加文章</div>");
			sb.append("<hr/>");
			sb.append("<div>").append(col.getDESCRIPTION()).append("</div>");
			sb.append("<div>");
		} else {
			sb.append(article2html(n));

		}
		return sb.toString();
	}

	public String article2html(VW_NEWSObj n) {
		INEWSDAO dao = new NEWSDAOImpl(getConnection());
		NEWSObj n0 = new NEWSObj();
		n0.setID(n.getID());
		n0.setVIEWER(n.getVIEWER() + 1);
		n0.setUPDATETIME(Util.getCurrentSQLTimestamp());
		try {
			dao.updateObjectByPK(n0);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		StringBuilder sb = new StringBuilder();

		sb.append("<div class='article-title'>").append(n.getTITLE())
				.append("</div>");
		sb.append("<div class='article-subtitle'>");
		if (n.getSHOWTIME() > 0) {
			// 显示时间
			sb.append("<span class='article-date'>"
					+ toDateString(n.getADDTIME()) + "</span>&nbsp;");

		} else {
			sb.append("<span class='article-date'>&nbsp;</span>");

		}
		sb.append("</div>");
		sb.append("<div class='article-text'>");

		// if (n.getICON() != null && n.getICON().length() > 0) {
		// String icon = n.getICON();
		// // icon = icon.substring(0, icon.lastIndexOf('.')) + "_s.jpg";
		// sb.append("<img class='article-image'  src='" + icon
		// + "'/>");
		// }
		sb.append(n.getCONTENT());
		sb.append("</div>");

		ATTACHMENTObjs atts = getAttatchment("NEWS" + n.getID());
		if (atts.size() > 0) {
			sb.append("<div class='article-attatchment'>");
			sb.append("<div class='article-attatchment-title'>附件</div>");
			sb.append("<ul >");
			for (ATTACHMENTObj a : atts) {
				sb.append("<li><a href='attatchment?tag=" + a.getTAG() + "'>")
						.append(a.getTITLE() + "(" + a.getDCOUNT()
								+ ")</a></li>");
			}
			sb.append("</div>");
		}
		sb.append("</ul>");
		sb.append("<div class='article-bottom'>");
		if (n.getORIGION() != null && n.getORIGION().length() > 0) {
			sb.append("来源:<span class='article-from'>" + n.getORIGION()
					+ "</span>&nbsp;&nbsp;&nbsp;&nbsp;");
		}
		if (n.getAUTHOR() != null && n.getAUTHOR().length() > 0) {
			sb.append("作者:<span class='article-author'>" + n.getAUTHOR()
					+ "</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		}
		if (n.getDUTYAUTHOR() != null && n.getDUTYAUTHOR().length() > 0) {
			sb.append("责编:<span class='article-dutyauthor'>"
					+ (n.getDUTYAUTHOR() == null ? "" : n.getDUTYAUTHOR())
					+ "</span>");
		}
		sb.append("</div>");
		sb.append("<div class='article-viewer'>总访问" + n.getVIEWER()
				+ "次[<span class='article-updatetime'>"
				+ this.toDateTimeString(n.getUPDATETIME()) + "</span>]</div>");

		return sb.toString();
	}

	public String columnlist2html(int cid, int subcid) {
		StringBuilder sb = new StringBuilder();
		COLUMNObj col = getColumn(cid);
		if (col == null) {
			return "<div style='border:solid 1px skyblue;padding:5px;'>系统不存在栏目:"
					+ cid + "</div>";
		}
		COLUMNObjs subcolumns = null;
		subcolumns = getChildColumns(cid);

		String id = "col" + cid + "";
		sb.append("<div id='" + id + "_tabs'>");

		String baseurl = "";
		if (col.getURL() != null && col.getURL().length() > 0) {
			baseurl = col.getURL();
		}
		sb.append("<div id='col" + col.getID() + "'>");
		String more = "";
		if (col.getURL() != null && col.getURL().length() > 0) {
			more = col.getURL();
		}
		sb.append("<div more='" + more + "' id='col" + col.getID() + "_title'>"
				+ col.getNAME() + "</div>");
		sb.append("<ul>");
		for (COLUMNObj c : subcolumns) {

			if (c.getURL() == null || c.getURL().length() == 0) {
				if (baseurl.length() > 0) {
					if (baseurl.lastIndexOf("?") > 0) {
						c.setURL(baseurl + "&subcid=" + c.getID());
					} else {
						c.setURL(baseurl + "?subcid=" + c.getID());
					}
				} else {
					c.setURL("");
				}
			}
			String current = "";

			if (c.getID() == subcid) {
				current = " class=\"current\" ";
			}

			sb.append("<li " + current + "><a title='" + c.getNAME()
					+ "' href='" + c.getURL() + "'>" + c.getNAME()
					+ "</a></li>");

		}
		sb.append("</ul>");
		sb.append("</div>");

		sb.append("</div>");
		return sb.toString();
	}

	/**
	 * 栏目 下面输出子栏目
	 * 
	 * @param cid
	 * @param count
	 * @return
	 */
	public String columncolumn2html(int cid, int count) {
		StringBuilder sb = new StringBuilder();

		COLUMNObj col = getColumn(cid);
		if (col == null) {
			return "<div style='border:solid 1px skyblue;padding:5px;'>系统不存在栏目:"
					+ cid + "</div>";
		}

		COLUMNObjs subcolumns = getChildColumns(cid);
		int total = Math.min(count, subcolumns.size());

		String id = "col" + cid + "";
		sb.append("<div id='" + id + "_tabs'>");
		String baseurl = "";
		if (col.getURL() != null && col.getURL().length() > 0) {
			baseurl = col.getURL();
		}

		sb.append("<div id='col" + col.getID() + "'>");
		String more = "";
		if (col.getURL() != null && col.getURL().length() > 0) {
			more = col.getURL();
		}
		sb.append("<div more='" + more + "' id='col" + col.getID() + "_title'>"
				+ col.getNAME() + "</div>");
		sb.append("<ul>");

		for (int i = 0; i < total; i++) {
			COLUMNObj c = subcolumns.get(i);

			if (c.getURL() == null || c.getURL().length() == 0) {
				if (baseurl.lastIndexOf("?") > 0) {
					c.setURL(baseurl + "&subcid=" + c.getID());
				} else {
					c.setURL(baseurl + "?subcid=" + c.getID());
				}
			}

			String img = "";
			if (c.getICON() != null && c.getICON().length() > 0) {
				img = c.getICON();
			}
			sb.append("<li><a title='" + c.getNAME() + "' href='" + c.getURL()
					+ "' img='" + img + "'>" + c.getNAME() + "</a><span>"
					+ toDateString(c.getCDATE()) + "</span></li>");
			sb.append("</li>");
		}

		sb.append("</ul>");
		sb.append("</div>");
		sb.append("</div>");
		return sb.toString();
	}

	public String makeurl(String url) {
		return response.encodeURL(url);
	}

	/**
	 * 
	 * @param cid
	 * @param count
	 * @param subcolumn
	 * @return
	 */
	public String column2html(int cid, PageNaviData data, boolean subcolumn,
			boolean showpicture, int order) {
		StringBuilder sb = new StringBuilder();

		COLUMNObj col = getColumn(cid);
		if (col == null) {
			return "<div style='border:solid 1px skyblue;padding:5px;'>系统不存在栏目:"
					+ cid + "</div>";
		}
		VW_NEWSObjs ns = null;
		COLUMNObjs subcolumns = null;
		if (subcolumn == true) {
			subcolumns = getChildColumns(cid);
		}

		ns = find_column_news(cid, data, false, order);

		String id = "col" + cid + "";
		sb.append("<div id='" + id + "_tabs'>");

		String baseurl = "";
		if (col.getURL() != null && col.getURL().length() > 0) {
			baseurl = col.getURL();
		}

		if (subcolumn == false) {
			sb.append(news_list(col, ns, showpicture));
		} else {
			sb.append(news_list(col, ns, showpicture));
			for (COLUMNObj c : subcolumns) {
				ns = find_column_news(c.getID(), data, false, order);
				if (c.getURL() == null || c.getURL().length() == 0) {
					if (baseurl.lastIndexOf("?") > 0) {
						c.setURL(baseurl + "&subcid=" + c.getID());
					} else {
						c.setURL(baseurl + "?subcid=" + c.getID());
					}
				}
				sb.append(news_list(c, ns, showpicture));
			}
		}
		sb.append("</div>");

		return sb.toString();
	}

	private String news_list(COLUMNObj c, VW_NEWSObjs ns, boolean showpicture) {
		StringBuilder sb = new StringBuilder();
		sb.append("<div id='col" + c.getID() + "'>");
		String more = "";
		if (c.getURL() != null && c.getURL().length() > 0) {
			more = c.getURL();
		}
		more = makeurl(more);
		sb.append("<div more='" + more + "' id='col" + c.getID() + "_title'>"
				+ c.getNAME() + "</div>");
		if (c.getCOLUMNTYPE() == ColumnType.CT_ARTICLE) { // 输出单篇文章
			sb.append("<div class='text'>");
			if (ns.size() > 0) {
				VW_NEWSObj b = ns.get(0);
				sb.append(b.getCONTENT());
			} else {
				sb.append("");
			}
			sb.append("</div>");
		} else if (ns.size() == 0) {
			sb.append("<div class='text'>");
			sb.append("");
			sb.append("</div>");
		} else {
			// 输出新闻列表

			sb.append("<ul>");
			for (VW_NEWSObj n : ns) {
				String link = "";
				if (n.getLINK() != null && n.getLINK().length() > 0) {
					link = n.getLINK();
				} else {
					link = "article/" + n.getID() + ".html";
				}
				link = makeurl(link);
				String icon = n.getICON();
				if (icon != null && icon.length() > 0) {
					icon = icon.substring(0, icon.lastIndexOf(".")) + "_s.jpg";
				}
				String picture = "";
				String summary = "";
				if (showpicture == true) {
					picture = "<img src='" + icon + "'>";
					String content = n.getSUMMARY();

					summary = "<div class='summary'>" + content + "</div>";
				}
				sb.append("<li>" + picture + "<a title='" + n.getTITLE()
						+ "' href='" + link + "' img='" + icon + "'  istop='"
						+ n.getUPTOP() + "' isnew='" + n.getLASTED() + "'>"
						+ n.getTITLE() + "</a><span>"
						+ toDateString(n.getADDTIME()) + "</span>" + summary
						+ "</li>");
			}
			sb.append("</ul>");
		}
		sb.append("</div>");
		return sb.toString();
	}

	private VW_NEWSObjs find_column_news(int cid, PageNaviData data,
			boolean content, int order) {
		VW_NEWSObjs ns = null;
		switch (order) {
		case ORDER_BY_RANK:
			ns = getColumnNewsByRank(cid, data, content);
			break;

		default:
			ns = getColumnNews(cid, data, content);
			break;
		}
		return ns;
	}

	/**
	 * 输出首页视频
	 * 
	 * @param cid
	 * @param count
	 * @param tag
	 * @return
	 */
	public String renderVideoColumn(int cid, int count, String tag) {
		StringBuilder sb = new StringBuilder();

		COLUMNObj col = getColumn(cid);
		if (col == null) {
			return "<div style='border:solid 1px skyblue;padding:5px;'>系统不存在栏目:"
					+ cid + "</div>";
		}

		String css = col.getCSS();
		if (css == null || css.length() == 0)
			css = "column";

		IVW_NEWSDAO dao = new VW_NEWSDAOImpl(getConnection());

		VW_NEWSObjs ns;
		try {
			ns = dao.getSpecificRecordByWhereTemplate(
					"where otype=? and columntype=?   order by addtime desc",
					0, count, OrgType.OT_NEWS, ColumnType.CT_VIDEO);
		} catch (SQLException e) {
			ns = new VW_NEWSObjs();
			e.printStackTrace();
		}

		String link = "";
		if (col.getURL() != null && col.getURL().length() > 0) {
			link = col.getURL();
		}
		String id = "col" + cid;
		sb.append("<div tag='" + tag + "' id='" + id + "' class=\"" + css
				+ "\" link='" + link + "'>");

		sb.append("<div id='" + id + "_top'>" + col.getNAME() + "</div>");
		sb.append("<ul id='" + id + "_ul'>");
		int index = 0;
		for (VW_NEWSObj n : ns) {
			// 图片连接
			link = "news/video.jsp?cid=" + cid + "&subcid=" + n.getOID()
					+ "&nid=" + n.getID();

			String icon = n.getICON();
			if (icon != null && icon.length() > 0) {
				icon = icon.substring(0, icon.lastIndexOf(".")) + "_s.jpg";
			}

			sb.append("<li><a  title='" + n.getTITLE() + "' href='" + link
					+ "' img='" + icon + "'>" + n.getTITLE() + "</a>");

			sb.append("<span>" + toDateString(n.getADDTIME()) + "</span></li>");

			index++;
		}
		sb.append("</ul></div>");
		return sb.toString();

	}

	/**
	 * 输出图片
	 * 
	 * @param cid
	 * @param count
	 * @param tag
	 * @return
	 */
	public String renderPictureColumn(int cid, int count, String tag) {
		StringBuilder sb = new StringBuilder();

		COLUMNObj col = getColumn(cid);
		if (col == null) {
			return "<div style='border:solid 1px skyblue;padding:5px;'>系统不存在栏目:"
					+ cid + "</div>";
		}

		String css = col.getCSS();
		if (css == null || css.length() == 0)
			css = "column";

		IVW_NEWSDAO dao = new VW_NEWSDAOImpl(getConnection());

		VW_NEWSObjs ns;
		try {
			ns = dao.getSpecificRecordByWhereTemplate(
					"where otype=? and columntype=? and oid<>35 order by addtime desc",
					0, count, OrgType.OT_NEWS, ColumnType.CT_PICTURE);
		} catch (SQLException e) {
			ns = new VW_NEWSObjs();
			e.printStackTrace();
		}

		String link = "";
		if (col.getURL() != null && col.getURL().length() > 0) {
			link = col.getURL();
		}
		String id = "col" + cid;
		sb.append("<div tag='" + tag + "' id='" + id + "' class=\"" + css
				+ "\" link='" + link + "'>");

		// sb.append("<div id='" + id + "_top'>" + col.getNAME() + "</div>");
		sb.append("<div id='" + id + "_top'>图片中心</div>");

		sb.append("<ul id='" + id + "_ul'>");
		int index = 0;
		for (VW_NEWSObj n : ns) {
			// 图片连接
			link = "news/tpzx.jsp?cid=36&subcid=" + n.getOID()
					+ "&viewtype=1&catid=" + n.getOID() + "&nid=" + n.getID();

			String icon = n.getICON();
			if (icon != null && icon.length() > 0) {
				icon = icon.substring(0, icon.lastIndexOf(".")) + "_s.jpg";
			}

			sb.append("<li><a  title='" + n.getTITLE() + "' href='" + link
					+ "' img='" + icon + "'>" + n.getTITLE() + "</a>");

			sb.append("<span>" + toDateString(n.getADDTIME()) + "</span></li>");

			index++;
		}
		sb.append("</ul></div>");
		return sb.toString();

	}

	public String renderColumnNewsSimple(int cid, int count, String tag) {
		StringBuilder sb = new StringBuilder();

		COLUMNObj col = getColumn(cid);
		if (col == null) {
			return "<div style='border:solid 1px skyblue;padding:5px;'>系统不存在栏目:"
					+ cid + "</div>";
		}

		String css = col.getCSS();
		if (css == null || css.length() == 0)
			css = "column";
		VW_NEWSObjs ns = getColumnNews(cid, count, false);
		String link = "";
		if (col.getURL() != null && col.getURL().length() > 0) {
			link = col.getURL();
		}
		String id = "col" + cid;
		sb.append("<div tag='" + tag + "' id='" + id + "' class=\"" + css
				+ "\" link='" + link + "'>");

		sb.append("<div id='" + id + "_top'>" + col.getNAME() + "</div>");
		sb.append("<ul id='" + id + "_ul'>");
		int index = 0;
		for (VW_NEWSObj n : ns) {
			if (col.getCOLUMNTYPE() == ColumnType.CT_VIDEO) {
				// 视频连接
				link = "news/video.jsp?cid=" + col.getID() + "&nid="
						+ n.getID();
			} else if (col.getCOLUMNTYPE() == ColumnType.CT_PICTURE) {
				// 图片连接
				link = col.getURL() + "&viewtype=1&imgindex=" + index;
			} else { // 文章连接
				link = "news/article.jsp?id=" + n.getID();
				if (n.getLINK() != null && n.getLINK().length() > 0) {
					link = n.getLINK();
				}
			}

			String icon = n.getICON();
			if (icon != null && icon.length() > 0) {
				icon = icon.substring(0, icon.lastIndexOf(".")) + "_s.jpg";
			}

			sb.append("<li><a  title='" + n.getTITLE() + "' href='" + link
					+ "' img='" + icon + "'>" + n.getTITLE() + "</a>");

			sb.append("<span>" + toDateString(n.getADDTIME()) + "</span></li>");

			index++;
		}
		sb.append("</ul></div>");
		return sb.toString();

	}

	public String renderScrollPictures(int cid, int count) {
		StringBuilder sb = new StringBuilder();

		COLUMNObj col = getColumn(cid);
		if (col == null) {
			return "<div style='border:solid 1px skyblue;padding:5px;'>系统不存在栏目:"
					+ cid + "</div>";
		}

		String css = col.getCSS();
		if (css == null || css.length() == 0)
			css = "column";

		VW_NEWSObjs ns = getColumnNews(cid, count, false);

		/*
		 * 处理栏目信息
		 */
		sb.append("<div class=\"" + css + "-top\">");
		sb.append("<span class=\"" + css + "-top-title\">")
				.append(col.getNAME()).append("</span>");
		if (col.getURL() != null && col.getURL().length() > 0) {

			sb.append("<span class=\"" + css + "-top-more\">");
			sb.append("<a class=\"" + css + "-top-more-link\" href='"
					+ col.getURL() + "'>");
			sb.append("</a>");
			sb.append("</span>");
		}
		sb.append("</div>");// end top

		sb.append("<div class=\"" + css + "-content\">"); // start content

		sb.append("<div class='" + css + "-content-leftbutton'></div>");

		sb.append("</div>"); // end content

		return sb.toString();
	}

	/**
	 * 
	 * @param cid
	 * @param count
	 * @return
	 */
	public String renderPictureColumn(String css, int cid, int count,
			boolean useThumbnail) {
		StringBuilder sb = new StringBuilder();

		COLUMNObj col = getColumn(cid);

		if (col == null) {
			return "<div style='border:solid 1px skyblue;padding:5px;'>系统不存在栏目:"
					+ cid + "</div>";
		}

		if (css == null) {
			css = col.getCSS();
			if (css == null || css.length() == 0)
				css = "column";
		}
		sb.append("<div id='piccol" + cid + "' class=\"" + css + "\"><ul>");

		VW_NEWSObjs ns = new VW_NEWSObjs();
		IVW_NEWSDAO dao = new VW_NEWSDAOImpl(getConnection());
		try {

			ns = dao.getSpecificRecordByWhereTemplate(
					"where oid=? and display>0 and (icon is not null) and length(icon)>0  order by addtime desc",
					0, count, cid);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		for (VW_NEWSObj n : ns) {
			sb.append("<li>");

			String url = n.getLINK();
			if (url == null || url.length() == 0) {
				url = "news/article.jsp?id=" + n.getID();
			}
			sb.append("<a target='_blank' href='" + url + "'>");

			if (useThumbnail == true) {
				String icon = n.getICON();
				if (icon != null && icon.length() > 0) {
					icon = icon.substring(0, icon.lastIndexOf(".")) + "_s.jpg";
				}
				sb.append("<img  width='272px' height='228px' alt='"
						+ n.getTITLE() + "' src='" + icon + "' />");
			} else {
				sb.append("<img  alt='" + n.getTITLE() + "' src='"
						+ n.getICON() + "' />");
			}
			sb.append("</a>");
			sb.append("</li>");
		}
		sb.append("</ul></div>");

		return sb.toString();
	}

	/**
	 * 
	 * @param cid
	 * @param count
	 * @return
	 */
	public String renderColumn2(int cid, int count) {
		StringBuilder sb = new StringBuilder();

		COLUMNObj col = getColumn(cid);
		COLUMNObjs subcols = getChildColumns(cid);
		if (col == null) {
			return "<div style='border:solid 1px skyblue;padding:5px;'>系统不存在栏目:"
					+ cid + "</div>";
		}

		String css = col.getCSS();
		if (css == null || css.length() == 0)
			css = "column";

		sb.append("<div class=\"" + css + "\">");

		/*
		 * 处理栏目信息
		 */
		sb.append("<div class=\"" + css + "-top\">");
		sb.append("<span class=\"" + css + "-top-title\">")
				.append(col.getNAME()).append("</span>");
		if (col.getURL() != null && col.getURL().length() > 0) {

			sb.append("<span class=\"" + css + "-top-more\">");
			sb.append("<a class=\"" + css + "-top-more-link\" href='"
					+ col.getURL() + "'>");
			sb.append("</a>");
			sb.append("</span>");
		}
		sb.append("</div>");// end top

		sb.append("<div class=\"" + css + "-content\">"); // start content

		String id = "ctabs_" + cid;

		sb.append("<div class='" + css + "-content-items'><ul>");
		VW_NEWSObjs ns = getColumnNewsByRank(cid, 100, false);
		for (VW_NEWSObj n : ns) {
			sb.append("<li>");
			sb.append("<a target='_blank' href='" + n.getLINK() + "'>");
			sb.append("<img src='" + n.getICON() + "' />");
			sb.append("</a>");
			sb.append(n.getTITLE());
			sb.append("</li>");
		}
		sb.append("</ul></div>");

		sb.append("<div class='" + css + "-content-items'>");

		System.out.println(subcols.size() + " column!");
		for (COLUMNObj c : subcols) {
			sb.append("<select id='select_" + c.getID() + "'>");
			ns = getColumnNewsByRank(c.getID(), 100, false);

			sb.append("<option value=''>");
			sb.append(c.getNAME());
			sb.append("</option>");

			for (VW_NEWSObj n : ns) {
				sb.append("<option value='" + n.getLINK() + "'>");
				sb.append(n.getTITLE());
				sb.append("</option>");
			}

			sb.append("</select>");
		}

		sb.append("</div>");

		sb.append("</div>"); // end content

		sb.append("</div>"); // end wrap
		return sb.toString();
	}

	/**
	 * 
	 * 
	 * @param cid
	 * @param count
	 * @return
	 */
	public String renderColumn(int cid, int count) {
		StringBuilder sb = new StringBuilder();

		COLUMNObj col = getColumn(cid);
		COLUMNObjs subcols = getChildColumns(cid);

		if (col == null) {
			return "<div style='border:solid 1px skyblue;padding:5px;'>系统不存在栏目:"
					+ cid + "</div>";
		}

		String css = col.getCSS();
		if (css == null || css.length() == 0)
			css = "column";

		sb.append("<div class=\"" + css + "\">");

		/*
		 * 处理栏目信息
		 */
		sb.append("<div class=\"" + css + "-top\">");
		sb.append("<span class=\"" + css + "-top-title\">")
				.append(col.getNAME()).append("</span>");
		if (col.getURL() != null && col.getURL().length() > 0) {

			sb.append("<span class=\"" + css + "-top-more\">");
			sb.append("<a class=\"" + css + "-top-more-link\" href='"
					+ col.getURL() + "'>");
			sb.append("</a>");
			sb.append("</span>");
		}
		sb.append("</div>");

		sb.append("<div class=\"" + css + "-content\">");

		String id = "ctabs_" + cid;
		if (subcols.size() > 0) {
			sb.append("<div id=\"" + id + "\" class=\"" + css
					+ "-content-tabs\">");
			sb.append("<ul>");

			/**
			 * 处理 TABS
			 */

			String css_prefix = css + "-content-tab";
			String css_selected = css + "-content-tab-selected";
			String css_unselected = css + "-content-tab-unselected";
			int index = 0;

			for (COLUMNObj c : subcols) {
				String c1 = (index == 0) ? css_selected : css_unselected;
				String tabid = "tab" + c.getID();
				sb.append("<li id=\"" + tabid + "\" tclass='" + css_prefix
						+ "' class='" + c1
						+ "' onmouseover='__tab_over(this,\"" + id + "\","
						+ (index) + ")'>");
				String url = "";
				if (c.getURL() != null && c.getURL().length() > 0) {
					url = c.getURL();
				} else {
					url = "javascript:void(0)";
				}
				sb.append("<a title='" + c.getNAME() + "' href=\"" + url
						+ "\">" + c.getNAME() + "</a>");
				sb.append("</li>");
				index++;
			}
			sb.append("</ul>");
			sb.append("</div>");

			index = 0;
			for (COLUMNObj c : subcols) {
				String show = index == 0 ? "display:block" : "display:none";
				String tabcontentid = id + "_" + (index++);
				sb.append("<div id=\"" + tabcontentid + "\" class=\"" + css
						+ "-list\" style='" + show + "'>");
				sb.append("<ul>");
				VW_NEWSObjs ns = getColumnNews(c.getID(), count, false);
				for (VW_NEWSObj n : ns) {
					sb.append("<li>");
					if (n.getLINK() != null && n.getLINK().length() > 0) {
						sb.append("<a href='" + n.getLINK() + "'>"
								+ n.getTITLE() + "</a>");
					} else {
						sb.append("<a  title='" + n.getTITLE()
								+ "' href='news/article.jsp?id=" + n.getID()
								+ "'>" + n.getTITLE() + "</a>");
					}
					sb.append("<span>" + toDateString(n.getADDTIME())
							+ "</span>");
					sb.append("</li>");
				}
				sb.append("</ul>");
				sb.append("</div>");
			}
		}

		else {
			sb.append("<div  class=\"" + css + "-list\">");
			sb.append("<ul>");
			VW_NEWSObjs ns = getColumnNews(cid, count, false);
			for (VW_NEWSObj n : ns) {
				sb.append("<li>");
				if (n.getLINK() != null && n.getLINK().length() > 0) {
					sb.append("<a href='" + n.getLINK() + "'>" + n.getTITLE()
							+ "</a>");
				} else {
					sb.append("<a href='news/article.jsp?id=" + n.getID()
							+ "'>" + n.getTITLE() + "</a>");
				}
				sb.append("<span>" + toDateString(n.getADDTIME()) + "</span>");
				sb.append("</li>");
			}
			sb.append("</ul>");
			sb.append("</div>");
		}

		sb.append("</div>");// end of content

		sb.append("</div>");
		return sb.toString();
	}

	public String toPagesNavigatorBar(PageNaviData data) {

		String baseurl = data.getBaseURL();
		long totalpage = data.getTotalPage();
		long currentpage = data.getCurrentPage();
		long pagesize = data.getPageSize();
		StringBuilder sb = new StringBuilder();
		sb.append("<ul id='page_navi_bar'>");

		String url = baseurl + "&pagesize=" + pagesize;
		if (totalpage <= 1) {

		} else if (totalpage < 10) {
			for (int i = 0; i < totalpage; i++) {
				String l = url + "&page=" + i;
				if (currentpage == i) {

					sb.append("<li><a href='" + l + "' class='current'>"
							+ (i + 1) + "</a></li>");
				} else {
					sb.append("<li><a href='" + l + "'>" + (i + 1)
							+ "</a></li>");
				}
			}
		} else {
			String first = "";
			String last = "";
			long start = 0;

			if (currentpage < 5) {
				start = 0;
				String l = url + "&page=" + (start + 10);
				last = "<li><a href='" + l + "'>...</a></li>";
				l = url + "&page=" + totalpage;
				last += "<li><a href='" + l + "'>尾页</a></li>";

			} else if (currentpage > totalpage - 5) {
				start = totalpage - 9;
				String l = url + "&page=0";
				first = "<li><a href='" + l + "'>首页</a></li>";
				l = url + "&page=" + (start - 1);
				first += "<li><a href='" + l + "'>...</a></li>";

			} else {
				start = currentpage - 9;
				String l = "";
				if (start > 0) {
					l = url + "&page=0";
					first = "<li><a href='" + l + "'>首页</a></li>";
					l = url + "&page=" + (start - 1);
					first += "<li><a href='" + l + "'>...</a></li>";
				} else {
					start = 0;
				}

				l = url + "&page=" + (start + 10);
				last = "<li><a href='" + l + "'>...</a></li>";
				l = url + "&page=" + totalpage;
				last += "<li><a href='" + l + "'>尾页</a></li>";

			}
			sb.append(first);
			for (long i = start; i <= start + 9; i++) {
				String l = url + "&page=" + i;
				if (currentpage == i) {

					sb.append("<li><a href='" + l + "' class='current'>"
							+ (i + 1) + "</a></li>");
				} else {
					sb.append("<li><a href='" + l + "'>" + (i + 1)
							+ "</a></li>");
				}
			}

			sb.append(last);

		}
		sb.append("</ul>");
		return sb.toString();
	}

	/**
	 * 返回请求的POST数据
	 * 
	 * @return
	 */
	public String getPostData() {
		StringBuilder sb = new StringBuilder();
		BufferedReader reader;
		try {
			reader = new BufferedReader(new InputStreamReader(
					request.getInputStream(), "UTF-8"));

			String line = reader.readLine();
			while (line != null) {
				sb.append(line);
				line = reader.readLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	/**
	 * 处理OA传来的新闻图片信息
	 * 
	 * @param images
	 */
	public void processOAImages(int nid, OAImage[] images) {
		if (images == null)
			return;

		for (OAImage img : images) {
			String fn = img.filename;
			int index = fn.lastIndexOf(".");
			if (index > 0) {
				String uploadDirectory = sureUploadDirectory();
				String filetype = fn.substring(index);
				String filename = Util.randomString(8) + filetype;

				String localname = request.getRealPath("upload") + "/"
						+ uploadDirectory + "/" + filename;
				try {
					Utility.decodeBase64(img.data, localname);
				} catch (IOException e) {
					e.printStackTrace();
				}

				if (img.type == 1) {
					// 保存附件
					IATTACHMENTDAO dao = new ATTACHMENTDAOImpl(
							Application.getConnection());
					ATTACHMENTObj att = new ATTACHMENTObj();
					att.setCDATE(Util.getCurrentSQLTimestamp());
					att.setDCOUNT(0);
					att.setTAG(Util.randomString(12));
					att.setFILEPATH("upload/" + uploadDirectory + "/"
							+ filename);
					att.setNID("NEWS" + nid);
					att.setFTYPE("");
					att.setTITLE(img.filename);
					try {
						dao.insertObject(att);
					} catch (SQLException e) {
						e.printStackTrace();
					}
				} else if (img.type == 0) { // 要替换文章的内容
					img.filename = "upload/" + uploadDirectory + "/" + filename;
				}
			}
		}
	}

	private String sureUploadDirectory() {
		Date now = new Date();
		String attach_subdir = "day_"
				+ Util.getDateString(now.getTime(), "yyMMdd");

		String temp = request.getRealPath("upload") + "/" + attach_subdir + "/";
		File tempFile = new File(temp);
		if (!tempFile.exists()) {
			tempFile.mkdirs();
		}
		return attach_subdir;
	}
}
